package cc.htdf.gwc.forecast.repository;

import com.google.common.base.Joiner;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by JT on 2017/10/9.
 */
@Repository
public class StationDataRepositoryImpl implements StationDataRepository {


    @Autowired
    private NamedParameterJdbcTemplate nameJdbcTemplate;

    private final static String TABLE_NAME_FORECAST_DATA = "eco_point_message_src";

    private final static String[] TABLE_COLUMNS_BASE = new String[]{
            "STATIONID", "LATITUDE", "LONGITUDE", "MODELRUNDATETIME", "VALIDDATETIME"
    };



    /**
     * 根据发布时间，预报时间段查询成都国家站预报数据
     *
     * @param statioins        : 站号
     * @param modelRunDateTime ：发布时间, 格式： “YYYYMMDDHH”
     * @param startValidTime   ：启始预报时间, 格式："yyyy-MM-dd HH:mm:ss"
     * @param endValidTime     ：结束预报时间, 格式："yyyy-MM-dd HH:mm:ss"
     * @param elements         : 要素
     * @return 预报数据
     */
    @Override
    public List<Map<String, Object>> findChengDuStatioinForecastDataByStationsAndModelAndValidTime(List<String> statioins, String modelRunDateTime, String startValidTime, String endValidTime, String elements) {
        String baseColumns = Joiner.on(',').join(TABLE_COLUMNS_BASE);
        String sql = String.format("SELECT %s,%s FROM %s WHERE STATIONID IN (:stationids) AND %s AND VALIDDATETIME >= :startValidTime AND VALIDDATETIME <= :endValidTime ",
                baseColumns, elements, TABLE_NAME_FORECAST_DATA, "DATE_FORMAT(str_to_date(MODELRUNDATETIME, '%Y-%m-%d %H:%i:%s'), '%Y%m%d%H') = :modelRunDateTime ");
        Map<String, Object> params = new HashMap<String, Object>(){{
            put("stationids", statioins);
            put("modelRunDateTime", modelRunDateTime);
            put("startValidTime", startValidTime);
            put("endValidTime", endValidTime);
        }};
        return nameJdbcTemplate.queryForList(sql, params);
    }

    @Override
    public List<Map<String, Object>> findChengDuStatioinForecastDataByStationsAndValidTime(List<String> stations, String validTime, String elements) {
        String baseColumns = Joiner.on(',').join(TABLE_COLUMNS_BASE);
        String sql = String.format("SELECT %s,%s FROM %s WHERE STATIONID IN (:stationids) AND VALIDDATETIME=:validTime",
                baseColumns, elements, TABLE_NAME_FORECAST_DATA);
        Map<String, Object> params = new HashMap<String, Object>(){{
            put("stationids", stations);
            put("validTime", validTime);
        }};
        return nameJdbcTemplate.queryForList(sql, params);
    }

    @Override
    public List<Map<String, Object>> findChengDuData(List<String> statioins, String modelRunDateTime, String startValidTime, String endValidTime, String elements) {
        String baseColumns = Joiner.on(',').join(TABLE_COLUMNS_BASE);
        String sql = String.format("SELECT %s,%s FROM %s WHERE STATIONID IN (:stationids) AND %s AND VALIDDATETIME >= :startValidTime AND VALIDDATETIME <= :endValidTime ",
                baseColumns, elements, TABLE_NAME_FORECAST_DATA, "MODELRUNDATETIME like :modelRunDateTime ");
        Map<String, Object> params = new HashMap<String, Object>(){{
            put("stationids", statioins);
            put("modelRunDateTime", modelRunDateTime);
            put("startValidTime", startValidTime);
            put("endValidTime", endValidTime);
        }};
        return nameJdbcTemplate.queryForList(sql, params);
    }

    /**
     * 根据台站、要素、发布小时，预报时次查询数据
     *
     * @param station   ：台站号
     * @param elements  ：要素
     * @param modelhour : 发布小时
     * @param times     ： 预报时次， 前多少小时
     * @return
     */
    @Override
    public List<Map<String, Object>> findChengDuDataByStationAndModelHourAndTimes(String station, String elements, String modelhour, String times) {
        String baseColumns = Joiner.on(',').join(TABLE_COLUMNS_BASE);
        String sql = String.format("SELECT %s, %s FROM %s WHERE STATIONID=:stationid %s %s",
                baseColumns, elements, TABLE_NAME_FORECAST_DATA, " AND MODELRUNDATETIME LIKE concat(concat('2%-%-% ', :modelhour), ':%') ",
                "AND DATE_ADD(STR_TO_DATE(ModelRunDateTime,'%Y-%m-%d %H:%i:%s'),INTERVAL :times HOUR)  >  STR_TO_DATE(ValidDateTime,'%Y-%m-%d %H:%i:%s')");

        Map<String, Object> params = new HashMap<String, Object>() {{
           put("stationid", station);
           put("modelhour", modelhour);
           put("times", String.valueOf(Integer.valueOf(times) - 1));
        }};

        return nameJdbcTemplate.queryForList(sql, params);
    }


}
